/*
XPTracker - Alternative Agile Tracking Tool
Copyright (C) 2006-2009  Stephen Starkey

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
*/
package org.xptracker.ant;

import jdepend.framework.JDepend;
import jdepend.framework.JavaPackage;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;

import java.io.IOException;
import java.util.Collection;

public class CycleCheckTask extends Task {
  private String path;

  public String getPath() {
    return path;
  }

  public void setPath(String path) {
    this.path = path;
  }

  public void execute() throws BuildException {
    if (path != null) {
      JDepend jdepend = new JDepend();
      try {
        jdepend.addDirectory(path);
        Collection packages = jdepend.analyze();
        if (jdepend.containsCycles()) {
          for(Object o: packages) {
            JavaPackage p = (JavaPackage) o;
            if (p.containsCycle()) {
              log(" * " + p.getName());
            }
          }
          throw new BuildException("Cycles Detected");
        }
      } catch (IOException e) {
        throw new BuildException(e);
      }
    }
  }
}
